package com.aimei520.service.impl;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.aimei520.mapper.AdminMapper;
import com.aimei520.mapper.LoginMapper;
import com.aimei520.po.admin.Access;
import com.aimei520.po.admin.Role;
import com.aimei520.po.admin.Url;
import com.aimei520.po.admin.User;
import com.aimei520.po.meun.Menu;
import com.aimei520.service.api.AdminService;
import com.aimei520.utils.Encryption;
import com.aimei520.utils.MenuTree;

@Service
public class AdminServiceImpl implements AdminService {

	@Resource
	private AdminMapper adminMapper;
	
	@Resource
	private LoginMapper loginMapper;
	
	@Override
	public void addRole(Role role,int[] urlids) {
		adminMapper.addRole(role);
		List<Access> addAccess = new ArrayList<Access>();
		for (int i = 0; i < urlids.length; i++) {
			Access access = new Access();
			access.setRoleId(role.getId());
			access.setUrlId(urlids[i]);
			addAccess.add(access);
		}
		if(addAccess.size()>0)
		adminMapper.addAccess(addAccess);
	}

	@Override
	public List<Role> roleList(Role role) {
		return adminMapper.queryRole(role);
	}

	@Override
	public void updateRole(Role role) {
		adminMapper.updateRole(role);
	}

	@Override
	public void deleteRole(Role role) {
		adminMapper.deleteRole(role);
	}

	@Override
	public void addUrl(Url url) {
		adminMapper.addUrl(url);
	}

	@Override
	public List<Url> urlList(Url url) {
		return adminMapper.queryUrl(url);
	}

	@Override
	public void updateUrl(Url url) {
		adminMapper.updateUrl(url);
	}

	@Override
	public void deleteUrl(int urlId) {
		Url url = new Url();
		url.setId(urlId);
		url.setStatus(1);
		adminMapper.updateUrl(url);
	}

	@Override
	public List<Url> headUrlList() {
		return adminMapper.queryHeadUrls();
	}

	@Override
	public List<Menu> urlList(int roleId) {
		MenuTree menuTree = new MenuTree();
		Url url = new Url();
		url.setStatus(1);
		
		List<Url> urls = adminMapper.queryUrl(url);
		List<Menu> accessUrls = new ArrayList<Menu>();
		if(roleId != 0)
			accessUrls = adminMapper.findAllMenu_byRoleId(roleId);
		
		return menuTree.urlList(urls,accessUrls);
	}

	@Override
	public void updateRoleAccess(Role role, int[] urlids) {
		List<Access> access = adminMapper.queryRoleAccess(role.getId());
		System.out.println(getSameAccess(access, urlids));
		
		if(getOnlyInAccess(access, urlids).size()>0)
			adminMapper.deleteAccess(getOnlyInAccess(access, urlids), role.getId());
		
		if(getOnlyInUrls(access, urlids,role.getId()).size()>0)
			adminMapper.addAccess(getOnlyInUrls(access, urlids,role.getId()));
		
		adminMapper.updateRole(role);
	}
	
	//找到url中有但是addcess中没有的 用于新增
	private List<Access> getOnlyInUrls(List<Access> access,int[] urlids,int roleId){
		List<Access> result = new ArrayList<Access>();
		List<Integer> list = Arrays.stream( urlids ).boxed().collect(Collectors.toList());
		List<Integer> remove = new ArrayList<Integer>();
		
		for (Access item : access) {
			remove.add(item.getUrlId());
		}
		
		list.removeAll(remove);
		
		for (int i = 0; i < list.size(); i++) {
			Access a = new Access();
			a.setRoleId(roleId);
			a.setUrlId(list.get(i));
			result.add(a);
		}
		
		return result;
	}
	
	//找到access 中有但是url中没有的 用于删除
	private List<Integer> getOnlyInAccess(List<Access> access,int[] urlids){
		List<Integer> remove = Arrays.stream( urlids ).boxed().collect(Collectors.toList());
		List<Integer> result = new ArrayList<Integer>();
		for (Access item : access) {
			result.add(item.getUrlId());
		}
		result.removeAll(remove);
		
		return result;
	}
	
	//查询出两个中都有的id
	private  List<Integer> getSameAccess(List<Access> access,int[] urlids) {
		List<Integer> result = new ArrayList<Integer>();
		
		for (int i = 0; i < urlids.length; i++) {
			for (int j = 0; j < access.size(); j++) {
				if(urlids[i] == access.get(j).getUrlId()) {
					result.add(urlids[i]);
					break;
				}
			}
		}
		
		return result;
	}

	
	/***************用户管理
	 * @throws NoSuchAlgorithmException **************/
	@Override
	public void addUser(User user) throws NoSuchAlgorithmException {
		user.setPassword(new Encryption(user.getPassword()).getEncryptionPassword());
		adminMapper.addUser(user);
	}

	@Override
	public List<User> userList(User user) {
		user.setStatus(1);
		return adminMapper.queryUser(user);
	}

	@Override
	public User user_byId(int id) {
		User user = new User();
		user.setId(id);
		return adminMapper.queryUser(user).get(0);
	}

	@Override
	public void updateUser(User user) throws NoSuchAlgorithmException {
		user.setPassword( new Encryption( user.getPassword()).getEncryptionPassword() );
		adminMapper.updateUser(user);
	}

	@Override
	public void deleteUser(int id) {
		User user = new User();
		user.setId(id);
		user.setStatus(2);
		adminMapper.updateUser(user);
	}
	

}
